package com.chinapost.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.chinapost.vo.Json;
import com.chinapost.vo.Yxjl;
import com.chinapost.vo.SessionInfo;
import com.chinapost.service.DeptServiceI;
import com.chinapost.service.YxjlServiceI;

@Action(value="yxjlAction",results = { @Result(name = "yxjlManager", location = "/jsp/yxjl/yxjlManager.jsp"), 
									   @Result(name = "yxjlAdd", location = "/jsp/yxjl/yxjlAdd.jsp"), 
									   @Result(name = "yxjlEdit", location = "/jsp/yxjl/yxjlEdit.jsp") })
public class YxjlAction extends BaseAction implements ModelDriven<Yxjl>, ServletRequestAware {
	
	private Yxjl yxjl=new Yxjl();
	private HttpServletRequest request;
	@Autowired
	private YxjlServiceI yxjlService;
	@Autowired
	private DeptServiceI deptService;
	

	@Override
	public Yxjl getModel() {
		return yxjl;
	}

	public String yxjlManager(){
		return "yxjlManager";
	}
	
	public String yxjlAdd(){
		return "yxjlAdd";
	}
	
	public String yxjlEdit(){
		return "yxjlEdit";
	}
	
	public void add(){
		Json j = new Json();
		try {
			yxjlService.add(yxjl);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}
	
	public void datagrid() {
		if (!StringUtils.isEmpty(yxjl.getDeptid())) {
			yxjl.setDeptIds(deptService.getChildIds(yxjl.getDeptid()));
		} else {
			SessionInfo s = (SessionInfo)request.getSession().getAttribute("sessionInfo");
			yxjl.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}

		super.writeJsonDate(yxjlService.datagrid(yxjl));
	}
	
	public void delete(){
		Json j = new Json();
		try {
			yxjlService.delete(yxjl);
			j.setSuccess(true);
			j.setMsg("删除成功！");
			j.setObj(yxjl.getCid());
		} catch (Exception e) {
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}
	
	public void edit(){
		Json j = new Json();
		try {
			yxjlService.edit(yxjl);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		writeJson(j);
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
	
}
